package cn.edu.jxau.core.util;

/**
 * Desc:类型转换工具类
 * ------------------------------------
 * Author:fulei04@meituan.com
 * Date:2017/12/14
 * Time:上午8:51.isDirectSign
 */
public class CastUtils {

    public static String cast2String(Object obj) {
        return cast2String(obj, "");
    }

    public static String cast2String(Object obj, String defaultVal) {
        return obj == null ? defaultVal : String.valueOf(obj);
    }

    public static Double cast2Double(Object obj) {
        return cast2Double(obj, 0.0);
    }

    public static Double cast2Double(Object obj, Double defaultVal) {

        String val = cast2String(obj);
        if (val.isEmpty()) {
            return defaultVal;
        } else {
            return Double.parseDouble(val);
        }
    }

    public static Long cast2Long(Object obj) {
        return cast2Long(obj, 0L);
    }

    public static Long cast2Long(Object obj,Long defaultVal) {

        String val = cast2String(obj);
        if(val.isEmpty()) {
            return defaultVal;
        } else {
            return Long.parseLong(val);
        }
    }

    public static Boolean cast2Boolean(Object obj) {
        return cast2Boolean(obj, false);
    }

    public static Boolean cast2Boolean(Object obj,Boolean defaultVal) {

        String val = cast2String(obj);
        if(val.isEmpty()) {
            return defaultVal;
        } else {
            return Boolean.parseBoolean(val);
        }
    }

    public static Integer cast2Int(Object obj) {
        return cast2Int(obj, 0);
    }

    public static Integer cast2Int(Object obj,Integer defaultVal) {

        String val = cast2String(obj);
        if(val.isEmpty()) {
            return defaultVal;
        } else {
            return Integer.parseInt(val);
        }
    }
}
